package com.itlong.cloud.property.service.impl;

import com.itlong.cloud.POJO.DTO.property.PropertyAddBankCardDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyBankManageGetPageDTO;
import com.itlong.cloud.POJO.PO.PropertyBankCardInfoPO;
import com.itlong.cloud.POJO.VO.property.BankCardInfoVO;
import com.itlong.cloud.POJO.VO.property.PropertyBankManageGetPageVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.property.dao.IPropertyBankCardManageDao;
import com.itlong.cloud.property.service.IPropertyBankCardManageService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      物业银行卡管理业务处理接口实现类
 * </desc>
 *
 * @createDate 2018/09/20
 */
@Service
public class IPropertyBankCardManageServiceImpl implements IPropertyBankCardManageService {
    @Autowired
    IPropertyBankCardManageDao IPropertyBankCardManageDao;

    /**
     * <desc>
     *      添加时获取物业银行手机号及支付密码信息
     * </desc>
     *
     * @param projectId
     * @return
     * @author Jiaqi.X
     * @createDate 2018/09/20
     */
    @Override
    public Map<String, Object> getBankPayConf(String projectId) {
        return IPropertyBankCardManageDao.getBankPayConf(projectId);
    }

    /**
     * <desc>
     *      验证银行账户唯一性
     * </desc>
     *
     * @param bankCardNo 银行卡号
     * @return
     * @author Jiaqi.X
     * @createDate 2018/09/20
     */
    @Override
    public Integer checkBankCardOnly(String bankCardNo,String bankCardId) {
        return IPropertyBankCardManageDao.checkBankCardOnly(bankCardNo,bankCardId);
    }

    /**
     * <desc>
     *      删除银行卡
     * </desc>
     *
     * @param bankCardId
     * @return
     * @author Jiaqi.X
     * @createDate 2018/09/20
     */
    @Override
    public Integer deleteBankCard(String bankCardId) {
        return IPropertyBankCardManageDao.deleteBankCard(bankCardId);
    }

    /**
     * <desc>
     *      获取银行卡账号详情
     * </desc>
     *
     * @param bankCardId
     * @return
     * @author Jiaqi.X
     * @createDate 2018/09/20
     */
    @Override
    public BankCardInfoVO getBankCardInfo(String bankCardId) {
        BankCardInfoVO vo = IPropertyBankCardManageDao.getBankCardInfo(bankCardId);
        vo.setPhoneNum(vo.getPhoneNum().substring(0,3)+"****"+vo.getPhoneNum().substring(7,11));
        return vo;
    }

    /**
     * <desc>
     *     查询项目下是否绑定手机号
     * </desc>
     *
     * @param projectId
     * @return
     * @author Jiaqi.X
     * @createDate 2018/09/20
     */
    @Override
    public String getProjectBankPhone(String projectId) {
        return IPropertyBankCardManageDao.getProjectBankPhone(projectId);
    }

    /**
     * <desc>
     *      添加或修改密码
     * </desc>
     *
     * @param propertyAddBankCardDTO
     * @return
     * @author Jiaqi.X
     * @createDate 2018/09/20
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public String addBankCard(PropertyAddBankCardDTO propertyAddBankCardDTO) throws Exception {
        PropertyBankCardInfoPO po = new PropertyBankCardInfoPO();
        po.setBankCardId(propertyAddBankCardDTO.getBankCardId());
        po.setBankCardNo(propertyAddBankCardDTO.getBankCardNo());
        po.setOpenBankName(propertyAddBankCardDTO.getOpenBankName());
        po.setUserName(propertyAddBankCardDTO.getUserName());
        po.setProv(propertyAddBankCardDTO.getProv());
        po.setCity(propertyAddBankCardDTO.getCity());
        po.setCheckStatus(1);
        po.setProjectId(propertyAddBankCardDTO.getProjectId());
        po.setStatus(DataStatusEnum.NORMAL.getType());
        po.setCreateTime(new Date());
        if(StringUtils.isBlank(po.getBankCardId())){
            if(StringUtils.isNotBlank(propertyAddBankCardDTO.getPayPassword())){
                //添加支付密码
                IPropertyBankCardManageDao.setPayPassword(propertyAddBankCardDTO.getProjectId(),propertyAddBankCardDTO.getPayPassword());
            }
            if(StringUtils.isNotBlank(propertyAddBankCardDTO.getPhoneNum()) &&
                    StringUtils.isBlank(IPropertyBankCardManageDao.getProjectBankPhone(propertyAddBankCardDTO.getProjectId()))){
                //添加手机号
                IPropertyBankCardManageDao.setBankPhoneNum(propertyAddBankCardDTO.getProjectId(),propertyAddBankCardDTO.getPhoneNum());
            }
            po.setBankCardId(LogicIdUtil.bussinessId());
            IPropertyBankCardManageDao.save(SqlUtil.durableData(po, PlatformConstants.TABLE_SAVE));
        }else{
            IPropertyBankCardManageDao.update(SqlUtil.durableData(po, PlatformConstants.TABLE_UPDATE));
        }
        return po.getBankCardId();
    }

    /**
     * <desc>
     *     获取抄银行卡账户列表
     * </desc>
     *
     * @param pageDTO   参数DTO
     * @return
     * @author Jiaqi.X
     * @createDate 2018/08/15
     */
    @Override
    public Page<PropertyBankManageGetPageVO> getPage(PropertyBankManageGetPageDTO pageDTO) throws Exception {
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO);
        //分页类默认pageSize为20
        Page<PropertyBankManageGetPageVO> page = new Page<>();
        page.getPage().setPageSize(pageDTO.getPageSize());
        page.getPage().setCurrentPage(pageDTO.getCurrentPage());
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        List<PropertyBankManageGetPageVO> list = IPropertyBankCardManageDao.getPage(params);
        //填充返回数据集
        page.setRows(list, IPropertyBankCardManageDao.getPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      获取银行名称
     * </desc>
     *
     * @param bankName
     * @return
     * @author Jiaqi.X
     * @createDate 2018/09/27
     */
    @Override
    public List<Map<String,Object>> getBanks(String bankName){
        return IPropertyBankCardManageDao.getBanks(bankName);
    }

    /**
     * <desc>
     *      获取物业银行卡列表
     * </desc>
     *
     * @param projectId
     * @return
     * @author Jiaqi.X
     * @createDate 2018/09/27
     */
    @Override
    public List<Map<String, Object>> getBankCards(String projectId) {
        return IPropertyBankCardManageDao.getBankCards(projectId);
    }

}
